package javango.forms.widgets;

import java.util.Map;

import javango.util.StringFormat12;

public abstract class AbstractChoicesWidget<K,V> extends AbstractWidget implements ChoicesWidget<K,V> {

	private String valueFormatString;
	private String keyFormatString;
	
	public AbstractChoicesWidget(Map<String, Object> attrs) {
		super(attrs);
	}

	private Map<K,V> choices;
	
	public Map<K,V> getChoices() {
		return choices;
	}

	public ChoicesWidget setChoices(Map<K,V> choices) {
		this.choices = choices;
		return this;
	}
	
	private boolean addBlankEntry = false;
	public ChoicesWidget setAddBlankEntry(boolean value) {
		addBlankEntry = value;
		return this;
	}
	
	public boolean isAddBlankEntry() {
		return addBlankEntry;
	}

	public void setValueFormatString(String valueFormatString) {
		this.valueFormatString = valueFormatString;
	}

	public void setKeyFormatString(String keyFormatString) {
		this.keyFormatString = keyFormatString;
	}

	public String getValueFormatString() {
		return valueFormatString;
	}

	public String getKeyFormatString() {
		return keyFormatString;
	}

	public String formatValue(Object value) {
		if (getValueFormatString() == null) {
			return value.toString();
		}
		return StringFormat12.format(getKeyFormatString(), value);
	}
	
	public String formatKey(Object key) {
		if (getKeyFormatString() == null) {
			return key.toString();
		}
		return StringFormat12.format(getKeyFormatString(), key);
	}

}
